package gridmapper;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ServerDataParser {

	public boolean writeData(DataOutputStream dataOut, ServerData serverData) {
		try {
			dataOut.writeInt(serverData.robotHeading);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		try {
			dataOut.writeBoolean(serverData.alive);
			dataOut.flush();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			return false;
		}
		return true;
	}

	public ServerData readData(DataInputStream dataIn) {
		int robotHeading = ServerData.ERROR_VALUE;
		boolean alive = false;
		try {
			robotHeading = dataIn.readInt();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		try {
			alive = dataIn.readBoolean();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		return new ServerData(robotHeading, alive);
	}
}
